package guda.stark.biz.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import guda.stark.biz.WaterRuleBiz;
import guda.stark.dal.dao.WaterRuleDAO;
import guda.stark.dal.dataobject.WaterRuleDO;
import guda.grape.autogen.common.page.BaseQuery;
import guda.grape.autogen.common.page.BizResult;

public class WaterRuleBizImpl implements WaterRuleBiz{

    private final static Logger logger = LoggerFactory.getLogger(WaterRuleBizImpl.class);

    @Autowired
    private WaterRuleDAO waterRuleDAO;

    public BizResult detail(long id) {
        BizResult bizResult = new BizResult();
        try{
            WaterRuleDO waterRuleDO = waterRuleDAO.getWaterRuleById(id);
            bizResult.data.put("waterRuleDO", waterRuleDO);
            bizResult.success = true;
        }catch(Exception e){
            logger.error("query WaterRule error",e);
        }
        return bizResult;
}

    public BizResult list(BaseQuery baseQuery) {
        BizResult bizResult = new BizResult();
        try {
            int totalCount = waterRuleDAO.countForPage(baseQuery);
            baseQuery.setTotalCount(totalCount);
            List<WaterRuleDO> waterRuleList = waterRuleDAO.selectForPage(baseQuery);
            bizResult.data.put("waterRuleList", waterRuleList);
            bizResult.data.put("query", baseQuery);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view WaterRule list error", e);
        }
            return bizResult;
     }

    public BizResult delete(long id) {
        BizResult bizResult = new BizResult();
        try {
            waterRuleDAO.delWaterRuleById(id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("delete waterRule error", e);
        }
        return bizResult;
    }

    public BizResult create(WaterRuleDO waterRuleDO) {
        BizResult bizResult = new BizResult();
        try {
            long id = waterRuleDAO.insert(waterRuleDO);
            bizResult.data.put("id", id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("create WaterRule error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult update(WaterRuleDO waterRuleDO) {
        BizResult bizResult = new BizResult();
        try {
            waterRuleDAO.updateByIdSelective(waterRuleDO);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update WaterRule error", e);
        }
        return bizResult;
    }


}
